#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

int minFlips(vector<vector<int>>& a) {
    int n = a.size(), m = a[0].size();
    int res = 0;
    for (int i = 0; i < n / 2; i++)
        for (int j = 0; j < m / 2; j++)
        {
            int cnt1 = a[i][j] + a[i][m - 1 - j] + a[n - 1 - i][j] + a[n - 1 - i][m - 1 - j];
            res += min(cnt1, 4 - cnt1);
        }

    if (n % 2 && m % 2)
        res += a[n / 2][m / 2];

    int diff = 0, cnt1 = 0;
    if (n % 2)
        for (int j = 0; j < m / 2; j++)
            if (a[n / 2][j] != a[n / 2][m - 1 - j])
                diff++;
            else    cnt1 += a[n / 2][j] * 2;
    if (m % 2)
        for (int i = 0; i < n / 2; i++)
            if (a[i][m / 2] != a[n - 1 - i][m / 2])
                diff++;
            else cnt1 += a[i][m / 2] * 2;

    return res + (diff ? diff : cnt1 % 4);
}